# Docker部署脚本
# 用于构建和部署所有服务到Docker

param(
    [string]$Environment = "dev",
    [switch]$Build,
    [switch]$Start,
    [switch]$Stop,
    [switch]$Restart,
    [switch]$Logs,
    [string]$Service = ""
)

$ErrorActionPreference = "Stop"

Write-Host "🐳 SmartCodeService Docker部署" -ForegroundColor Cyan
Write-Host "================================" -ForegroundColor Cyan
Write-Host ""

# 选择docker-compose文件
$composeFile = if ($Environment -eq "test") {
    "docker-compose.test.yml"
} else {
    "docker-compose.yml"
}

if ($Build) {
    Write-Host "📦 构建Docker镜像..." -ForegroundColor Yellow
    docker-compose -f $composeFile build
    
    if ($LASTEXITCODE -eq 0) {
        Write-Host "✅ 构建成功" -ForegroundColor Green
    } else {
        Write-Host "❌ 构建失败" -ForegroundColor Red
        exit 1
    }
    Write-Host ""
}

if ($Start) {
    Write-Host "🚀 启动服务..." -ForegroundColor Yellow
    
    if ($Service) {
        docker-compose -f $composeFile up -d $Service
    } else {
        docker-compose -f $composeFile up -d
    }
    
    if ($LASTEXITCODE -eq 0) {
        Write-Host "✅ 服务启动成功" -ForegroundColor Green
        
        # 等待服务就绪
        Write-Host "⏳ 等待服务就绪..." -ForegroundColor Yellow
        Start-Sleep -Seconds 10
        
        # 检查服务状态
        Write-Host ""
        Write-Host "📊 服务状态:" -ForegroundColor Cyan
        docker-compose -f $composeFile ps
    } else {
        Write-Host "❌ 服务启动失败" -ForegroundColor Red
        exit 1
    }
    Write-Host ""
}

if ($Stop) {
    Write-Host "🛑 停止服务..." -ForegroundColor Yellow
    
    if ($Service) {
        docker-compose -f $composeFile stop $Service
    } else {
        docker-compose -f $composeFile stop
    }
    
    Write-Host "✅ 服务已停止" -ForegroundColor Green
    Write-Host ""
}

if ($Restart) {
    Write-Host "🔄 重启服务..." -ForegroundColor Yellow
    
    if ($Service) {
        docker-compose -f $composeFile restart $Service
    } else {
        docker-compose -f $composeFile restart
    }
    
    Write-Host "✅ 服务已重启" -ForegroundColor Green
    Write-Host ""
}

if ($Logs) {
    Write-Host "📋 查看日志..." -ForegroundColor Yellow
    
    if ($Service) {
        docker-compose -f $composeFile logs -f $Service
    } else {
        docker-compose -f $composeFile logs -f
    }
}

# 如果没有指定任何操作，显示帮助
if (-not ($Build -or $Start -or $Stop -or $Restart -or $Logs)) {
    Write-Host "用法:" -ForegroundColor Yellow
    Write-Host "  .\deploy-docker.ps1 -Build              # 构建镜像"
    Write-Host "  .\deploy-docker.ps1 -Start              # 启动所有服务"
    Write-Host "  .\deploy-docker.ps1 -Start -Service gateway  # 启动特定服务"
    Write-Host "  .\deploy-docker.ps1 -Stop               # 停止所有服务"
    Write-Host "  .\deploy-docker.ps1 -Restart            # 重启所有服务"
    Write-Host "  .\deploy-docker.ps1 -Logs               # 查看日志"
    Write-Host "  .\deploy-docker.ps1 -Build -Start       # 构建并启动"
    Write-Host ""
    Write-Host "环境:" -ForegroundColor Yellow
    Write-Host "  -Environment dev   # 开发环境（默认）"
    Write-Host "  -Environment test  # 测试环境"
}

